<template>
  <div class="userProfile">
    <van-nav-bar
      left-arrow
      title="个人信息"
      @click-left="$router.push('/user')"
    ></van-nav-bar>
    <van-cell-group>
      <van-uploader
        style="width:100%"
        :before-read="beforeRead"
        :after-read="afterRead"
      >
        <van-cell title="头像" is-link>
          <img class="img" :src="userInfo.photo" alt="" />
        </van-cell>
      </van-uploader>
      <van-cell
        title="昵称"
        @click="nameClick"
        :value="userInfo.name"
        center
        is-link
      ></van-cell>
    </van-cell-group>
    <van-cell-group style="margin-top:12px">
      <van-cell
        @click="genderClick"
        title="性别"
        center
        is-link
        :value="userInfo.gender === 1 ? '女' : '男'"
      >
      </van-cell>
      <van-cell
        @click="birthdayClick"
        title="生日"
        center
        is-link
        :value="userInfo.birthday | fliterTime"
      >
      </van-cell>
    </van-cell-group>
    <div class="logout" @click="logout">
      <span>退出登录</span>
    </div>

    <!-- 动作面板 -->
    <van-action-sheet
      v-model="showGender"
      :actions="actions"
      cancel-text="取消"
      close-on-click-action
      @cancel="showGender = false"
      @select="genderSelect"
    ></van-action-sheet>
    <!-- 时间 -->
    <van-popup v-model="showDate" position="bottom">
      <van-datetime-picker
        v-model="currentTime"
        title="选择生日"
        type="date"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="showDate = false"
        @confirm="dateConfirm"
      ></van-datetime-picker>
    </van-popup>
    <!-- 昵称 -->
    <EditName ref="editName"></EditName>
  </div>
</template>

<script>
import EditName from './components/editName.vue'
import { userProfilePatch, userPhoto } from '@/api/user.js'
import { mapState } from 'vuex'
import dayjs from 'dayjs'
export default {
  name: 'userProfile',
  components: {
    EditName
  },
  filters: {
    fliterTime (time) {
      return dayjs(time).format('YYYY-MM-DD')
    }
  },
  computed: {
    ...mapState('user', ['userInfo'])
  },
  created () {
    this.$store.dispatch('user/getUserInfo')
  },
  data () {
    return {
      showDate: false,
      currentTime: new Date(),
      minDate: new Date('1920-03-04'),
      maxDate: new Date(),
      showGender: false,
      actions: [
        {
          name: '男',
          value: 0
        },
        {
          name: '女',
          value: 1
        }
      ]
    }
  },
  methods: {
    // 退出
    logout () {
      this.$dialog
        .confirm({
          title: '退出登录',
          message: '确定要退出吗'
        })
        .then(() => {
          this.$store.commit('user/setToken', '')
          this.$router.push('/')
          this.$toast.success('退出成功')
        })
        .catch(() => {})
    },
    // 修改信息方法
    async editUserInfo (obj) {
      await userProfilePatch({
        [obj.key]: obj.value
      })
      this.$store.commit('user/setUserOther', [[obj.key], obj.value])
      this.$toast.success('修改成功')
    },
    // 修改昵称
    nameClick () {
      this.$refs.editName.showName = true
      this.$refs.editName.inputVal = this.userInfo.name
    },
    // 选择性别
    async genderSelect (item) {
      this.editUserInfo({
        key: 'gender',
        value: item.value
      })
    },
    // 性别点击
    genderClick () {
      this.showGender = true
      this.actions[this.userInfo.gender].disabled = true
      this.actions[1 - this.userInfo.gender].disabled = false
    },
    // 生日点击
    birthdayClick () {
      this.showDate = true
      this.currentTime = new Date(this.userInfo.birthday)
    },
    // 确定修改生日
    dateConfirm () {
      this.showDate = false
      this.editUserInfo({
        key: 'birthday',
        value: this.currentTime
      })
    },
    // 修改头像 读取后
    async afterRead (res) {
      const _form = new FormData()
      _form.append('photo', res.file)
      const response = await userPhoto(_form)
      this.$store.commit('user/setUserOther', ['photo', response.data.photo])
      this.$toast.success('修改成功')
    },
    // 读取前
    beforeRead (file) {
      const isImg = file.type === 'image/png' || file.type === 'image/jpeg'
      const limit = file.size / 1024 / 1204 < 1
      if (!isImg) {
        this.$toast.fail('请上传png或者jpeg格式图片')
      }
      if (!limit) {
        this.$toast.fail('请上传1M内图片')
      }
      return isImg && limit
    }
  }
}
</script>

<style lang="less" scoped>
.userProfile {
  background-color: #f8f8f8;
  .img {
    width: 30px;
    height: 30px;
    border-radius: 50%;
  }
}
::v-deep .van-nav-bar .van-icon {
  color: #999;
}
.logout {
  text-align: center;
  position: absolute;
  left: 0;
  bottom: 30px;
  width: 100%;
  color: @geek-color;
}
::v-deep .van-uploader__input-wrapper {
  width: 100%;
}
</style>
